package com.nbjtjc.safe.api.cost;

import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.nbjtjc.safe.annotation.SysLog;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.mapper.cost.CostBudgetMonthMapper;
import com.nbjtjc.safe.model.cost.CostBudgetMonth;
import com.nbjtjc.safe.model.cost.CostBudgetPlan;
import com.nbjtjc.safe.model.cost.CostInjuryClaim;
import com.nbjtjc.safe.service.cost.CostAccountRecordService;
import com.nbjtjc.safe.service.cost.CostBudgetPlanService;

@RestController
@CrossOrigin("*")
@RequestMapping("/costBudgetPlan")
public class CostBudgetPlanController {

	@Autowired
	private CostBudgetPlanService costBudgetPlanService;
	@Autowired
	  private CostBudgetMonthMapper costBudgetMonthMapper;
	@Autowired
	private CostAccountRecordService costAccountRecordService;

	  @RequestMapping(value = "/addOrUpdate",method = {RequestMethod.POST})
//	  @SysLog("添加或更新安全投入预算计划")
	    public Result add(@RequestBody CostBudgetPlan costBudgetPlan){
	        Result result=new Result();
	        try {
	            result=costBudgetPlanService.addByTemplet(costBudgetPlan);
	        } catch (Exception e) {
	            e.printStackTrace();
	            result.setFalse(e.getMessage());
	        }
	        return result;
	    }
	  @RequestMapping(value = "/updatePlanMain",method = {RequestMethod.POST})
	    public Result updatePlanMain(@RequestBody CostBudgetPlan costBudgetPlan){
	        Result result=new Result();
	        try {
	            result=costBudgetPlanService.updatePlanMain(costBudgetPlan);
	        } catch (Exception e) {
	            e.printStackTrace();
	            result.setFalse(e.getMessage());
	        }
	        return result;
	    }
	  
//		  @RequestMapping(value = "/updateMonth",method = {RequestMethod.POST})
////		  @SysLog("添加或更新安全投入预算计划")
//		    public Result add(@RequestBody CostBudgetMonth costBudgetMonth){
//		        Result result=new Result();
//		        try {
//		    		 costBudgetMonthMapper.updateByPrimaryKeySelective(costBudgetMonth);
//		    		
//		        } catch (Exception e) {
//		            e.printStackTrace();
//		            result.setFalse(e.getMessage());
//		        }
//		        result.setData(costBudgetMonth);
//		        return result;
//		    }
	  
	  @RequestMapping(value = "/delete",method = {RequestMethod.POST})
	  @SysLog("删除安全投入预算计划")
	    public Result delete(@RequestBody CostBudgetPlan costBudgetPlan) {
	        Result result=new Result();
	        try {
	            result=costBudgetPlanService.delete(costBudgetPlan);
	        } catch (Exception e) {
	            e.printStackTrace();
	            result.setFalse(e.getMessage());
	        }
	        return result;
	    }

	  @RequestMapping(value = "/find", method = {RequestMethod.POST})
	  public Result find(@RequestBody CostBudgetPlan costBudgetPlan) {
	        Result result = new Result();

	        try {
	            result = costBudgetPlanService.find(costBudgetPlan);
	        } catch (Exception e) {
	            e.printStackTrace();
	            result.setFalse(e.getMessage());
	        }
	        return result;
	    }
	  
	  @RequestMapping(value = "/findDetail", method = {RequestMethod.POST})
	  public Result findDetail(@RequestBody CostBudgetPlan costBudgetPlan) {
	        Result result = new Result();

	        try {
	            result = costBudgetPlanService.findDetail(costBudgetPlan);
	        } catch (Exception e) {
	            e.printStackTrace();
	            result.setFalse(e.getMessage());
	        }
	        return result;
	    }
	  
	  @RequestMapping(value = "/findSubcompany", method = {RequestMethod.POST})
	  public Result findSubcompany(@RequestBody CostBudgetPlan costBudgetPlan) {
	        Result result = new Result();

	        try {
	            result = costBudgetPlanService.findSubcompany(costBudgetPlan);
	        } catch (Exception e) {
	            e.printStackTrace();
	            result.setFalse(e.getMessage());
	        }
	        return result;
	    }
	  
	  @RequestMapping(value = "/findSubcompanyItems", method = {RequestMethod.POST})
	  public Result findSubcompanyItems(@RequestBody CostBudgetPlan costBudgetPlan) {
	        Result result = new Result();

	        try {
	            result = costBudgetPlanService.findSubcompanyItems(costBudgetPlan);
	        } catch (Exception e) {
	            e.printStackTrace();
	            result.setFalse(e.getMessage());
	        }
	        return result;
	    }
	  
	  @RequestMapping(value = "/findTrackSubcompany", method = {RequestMethod.POST})
	  public Result findTrackSubcompany(@RequestBody CostBudgetPlan costBudgetPlan) {
	        Result result = new Result();

	        try {
	            result = costBudgetPlanService.findTrackSubcompany(costBudgetPlan);
	        } catch (Exception e) {
	            e.printStackTrace();
	            result.setFalse(e.getMessage());
	        }
	        return result;
	    }
	  
	  @RequestMapping(value = "/searchByYear", method = {RequestMethod.POST})
	  public Result searchByYear(@RequestBody CostBudgetPlan costBudgetPlan) {
	        Result result = new Result();

	        try {
	            result = costBudgetPlanService.searchByYear(costBudgetPlan);
	            
	        } catch (Exception e) {
	            e.printStackTrace();
	            result.setFalse(e.getMessage());
	        }
	        return result;
	    }
	  

	  
	  @RequestMapping(value = "/show", method = {RequestMethod.POST})
	  public Result show(@RequestBody CostBudgetPlan costBudgetPlan) {
	        Result result = new Result();

	        try {
	            result = costBudgetPlanService.show(costBudgetPlan.getId());
	        } catch (Exception e) {
	            e.printStackTrace();
	            result.setFalse(e.getMessage());
	        }
	        return result;
	    }
	  
//	    @SysLog("导入安全费用预算计划")
//	    @RequestMapping(value = "/addBatch",method = {RequestMethod.POST})
//	    public Result addBatch(MultipartFile multipartFile,@RequestBody CostBudgetPlan costBudgetPlan){
//	        Result result=new Result();
//	        try {
//		        result = costBudgetPlanService.addBatch(multipartFile,costBudgetPlan); 
//	        } catch (Exception e) {
//	            e.printStackTrace();
//	            result.setFalse(e.getMessage());
//	        }
//	        return result;
//	    }
	    
	    @SysLog("导入安全费用预算计划")
	    @RequestMapping(value = "/addBatch",method = {RequestMethod.POST})
	    public Result addBatch(HttpServletRequest request, CostBudgetPlan costBudgetPlan){
	    	 Result result = new Result();
	         try {
	             // 解析器解析request的上下文未登录或者过期
	             CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
	             // 先判断request中是否包含mutipart类型的数据
	             if (multipartResolver.isMultipart(request)) {
	                 // 再将request中得数据转化成multipart类型的数据f
	                 MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
	                 List<MultipartFile> multipartFiles = multiRequest.getFiles("file");
	                 Iterator iterator = multiRequest.getFileNames();
	                 while (iterator.hasNext()) {
	                     MultipartFile file = multiRequest.getFile((String) iterator.next());
	                     result = costBudgetPlanService.addBatch(file, costBudgetPlan);
	                     if (result.isSuccess()) {
	                     } else {
	                         result.setFalse(result.getMessage());
	                     }
	                 }
	             }
	         } catch (Exception e) {
	             e.printStackTrace();
	             result.setFalse("导入失败" + e.getMessage());
	         }
	         return result;
	    }
	   
}
